<template>
  <div>
    <el-dialog
      v-model="visibleDialog"
      :title="props.title"
      width="600px"
      draggable
      @close="closeDialog"
      align-center
    >
      <el-form
        ref="formRef"
        :model="formData"
        :inline="true"
        :rules="rules"
        label-width="140"
        v-show="!props.isReadonly"
      >
        <el-row>
          <el-col :span="24">
            <el-form-item
              label="过程监控分类"
              style="width: 100%"
              prop="abilityType"
            >
              <el-select
                v-model="formData.abilityType"
                placeholder="请选择过程监控分类"
                clearable
                v-if="!props.isReadonly"
                style="width: 100%"
              >
                <el-option
                  v-for="item in useDictionary('AbilityType')"
                  :key="item.value"
                  :label="item.label"
                  :value="Number(item.value)"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item
              label="报警通知内容设定"
              prop="noticeContent"
              style="width: 100%"
            >
              <el-input
                style="width: 100%"
                v-if="!props.isReadonly"
                v-model="formData.noticeContent"
                placeholder="请输入报警通知内容设定"
                clearable
                type="textarea"
                :autosize="{ minRows: 3, maxRows: 4 }"
              />
              <span v-else>{{ formData.noticeContent }}</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-tag
            v-for="(item, index) in parmList"
            :key="index"
            @click="intpuparm(item)"
            class="mr-[9px] mb-[12px] cursor-pointer"
            >{{ item.name }}</el-tag
          >
          <!-- <el-col :span="3" v-for="item in parmList" :key="index">
            <el-button type="primary" size="small" link @click="intpuparm(item)"
              >{{ item.name }}
            </el-button>
          </el-col> -->
          <el-form-item />
        </el-row>
      </el-form>
      <el-descriptions
        class="margin-top"
        title=""
        :column="1"
        border
        v-show="props.isReadonly"
      >
        <el-descriptions-item>
          <template #label>
            <div class="cell-item">过程监控分类</div>
          </template>

          <div style="width: 100%; word-break: break-all">
            {{
              dictionaryStore.filterDictName(
                "AbilityType",
                formData.abilityType
              )
            }}
          </div>
        </el-descriptions-item>
        <el-descriptions-item>
          <template #label>
            <div class="cell-item">报警通知内容设定</div>
          </template>

          <div style="width: 100%; word-break: break-all">
            {{ formData.noticeContent }}
          </div>
        </el-descriptions-item>
      </el-descriptions>
      <template #footer>
        <div class="dialog-footer">
          <el-button
            v-if="!props.isReadonly"
            type="primary"
            :loading="loading"
            @click="handleSubmit"
            >确 定
          </el-button>
          <el-button @click="closeDialog">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import request from "@/utils/request";
import { useDictStoreHook } from "@/store/modules/dictionary"; // 引入
import { useDictionary } from "@/utils/useTable";

const dictionaryStore = useDictStoreHook();

defineOptions({
  name: "PCMPCMNOTICESETTINGADDORUPDATE",
  inheritAttrs: false,
});

const emit = defineEmits(["reset-table", "change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  title: {
    type: String,
    default: () => {
      return "";
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
  isReadonly: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
});

/** 弹窗显示 **/
const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
    if (newVal) {
      getParmList();
    }
    setTimeout(() => {
      formRef.value.clearValidate();
    }, 50);
  }
);

/** formData定义表单数据 */
const formRef = ref(ElForm);
let formData = reactive({
  abilityType: "",
  createTime: "",
  createdUserId: "",
  createdUserName: "",
  lastUpdatedUserId: "",
  lastUpdatedUserName: "",
  lastUpdateTime: "",
  noticeContent: "",
  id: "",
  details: [
    {
      noticeSettingId: "",
      email: "",
      id: "",
    },
  ],
  parmList: [],
});

/** rules规则 */
const rules = reactive({
  abilityType: [
    { required: true, message: "请选择过程监控分类", trigger: "change" },
  ],
  createTime: [],
  createdUserId: [],
  createdUserName: [],
  lastUpdatedUserId: [],
  lastUpdatedUserName: [],
  lastUpdateTime: [],
  noticeContent: [
    { required: true, message: "请输入报警通知内容设定", trigger: "blur" },
  ],
  sonnoticeSettingId: [],
  sonemail: [
    { required: true, message: "邮箱不能为空", trigger: "blur" },
    {
      type: "email",
      message: "请输入正确的邮箱格式（如：user@example.com）",
      trigger: ["blur", "change"],
    },
  ],
});

const isValidated = ref(false);
/** 删除行 */
const handleDelete = (index: any) => {
  formData.details.splice(index, 1);
  isValidated.value && formRef.value.validate(() => {});
};

const details = {
  noticeSettingId: "",
  email: "",
  id: "",
};

/** 添加行 */
const handleAdd = () => {
  let sondata = JSON.parse(JSON.stringify(details));
  formData.details.push(sondata);
};

/** 编辑时赋值 */
watch(
  () => props.data,
  (newVal: any) => {
    formData.id = "";
    if (newVal?.id) {
      Object.assign(formData, props.data);
      if (formData.id) {
        getdetails();
      }
    }
  }
);

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
  resetForm();
}

/** 重置表单 */
function resetForm() {
  formRef.value.resetFields();
  formRef.value.clearValidate();
  formData.details = [JSON.parse(JSON.stringify(details))];
  formData.abilityType = "";
  formData.createTime = "";
  formData.createdUserId = "";
  formData.createdUserName = "";
  formData.lastUpdatedUserId = "";
  formData.lastUpdatedUserName = "";
  formData.lastUpdateTime = "";
  formData.noticeContent = "";
  formData.id = "";
}

const loading = ref(false);

/** ${comments}保存提交 */
function handleSubmit() {
  formRef.value.validate(async (valid: any) => {
    if (valid) {
      loading.value = true;
      const text = formData.id ? "编辑" : "新增";
      console.log("formData", toRaw(formData));
      await request({
        url: "/dmpDosPcm/pcmnoticesetting/save",
        method: "post",
        data: formData,
      }).finally(() => {
        loading.value = false;
      });
      ElMessage.success(text + `成功`);
      closeDialog();
      emit("reset-table", formData.id);
      isValidated.value = false;
    } else {
      isValidated.value = true;
    }
  });
}

function intpuparm(item: any) {
  formData.noticeContent = formData.noticeContent + item.code;
  console.log(formData.noticeContent);
}

async function getdetails() {
  let sondetails = await request({
    url: "/dmpDosPcm/pcmnoticesetting/getDetails",
    method: "post",
    data: formData,
  }).finally(() => {
    loading.value = false;
  });
  formData.details = sondetails.data;
}

onMounted(() => {});
const parmList = ref([]);

async function getParmList() {
  let retparmList = await request({
    url: "/dmpDosPcm/pcmnoticesetting/getParmList",
    method: "post",
    data: {},
  }).finally(() => {
    loading.value = false;
  });
  console.log("1111111asdasdasd");
  parmList.value = retparmList.data;

  console.log(parmList);
}
</script>

<style lang="scss" scoped>
.formTable {
  :deep(.el-form-item) {
    margin: 0 0 13px 0 !important;
    width: 100%;
  }
}

:deep(.el-dialog .formTable .el-table .el-form-item) {
  padding: 11px 0 0 0 !important;
}

:deep(.el-dialog .formTable .el-table .el-form-item__error) {
  font-size: 11px !important;
}

:deep(.el-dialog .formTable .el-table) {
  // .cell.el-tooltip {
  //   padding: 0 15px;
  // }

  // .el-table__body td.el-table__cell {
  //   padding: 20;
  // }
  // .cell.el-tooltip {
  //   padding: 5px;
  // }
  .el-table__body td.el-table__cell {
    // padding: 5px !important;
    padding: 0 !important;
  }
}

// :deep(.el-dialog .el-dialog__body) {
//   overflow-y: auto;
//   height: 65vh;
// }

// :deep(.search-container .el-input.el-input--default.el-input--suffix) {
//   // 固定宽度
//   width: 190px !important;
// }

// :deep(.search-container .el-form-item--default .el-form-item__label) {
//   // 固定宽度
//   justify-content: end;
// }

:deep(.el-descriptions__label.el-descriptions__cell.is-bordered-label) {
  width: 160px;
}

:deep(.el-descriptions__content.el-descriptions__cell.is-bordered-content) {
  min-width: 180px;
}
</style>
